//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;
using System.Collections.Generic;
using System;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal partial class WorkflowActivitiesUserControl : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowActivitiesUserControl"/> class.
        /// </summary>
        public WorkflowActivitiesUserControl()
        {
            InitializeComponent();
            this.ctrWorkflowChildren.SelectedIndexChanged += new EventHandler<EventArgs>(ctrWorkflowChildrenActivities_SelectedIndexChanged);
            this.ctrWorkflowChildren.DoubleClick += new EventHandler(ctrWorkflowChildrenActivities_DoubleClick);
        }

        /// <summary>
        /// Occurs when [on selected activity changed].
        /// </summary>
        public event EventHandler<SelectedActivityChangedEventArgs> OnSelectedActivityChanged;

        /// <summary>
        /// Occurs when [on activity double click].
        /// </summary>
        public event EventHandler<SelectedActivityChangedEventArgs> OnActivityDoubleClick;

        /// <summary>
        /// Refreshes the specified workflow.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        public void Refresh(Workflow workflow)
        {
            if (workflow != null)
            {
                int activityCount = workflow.Activities.Count;
                ctrWorkflowChildren.WorkflowDescription = workflow.Description;
                ctrWorkflowChildren.WorkflowName = workflow.Name;
                List<WorkflowChild> activityNames = new List<WorkflowChild>(activityCount);
                for (int i = 0; i < activityCount; i++)
                {
                    activityNames.Add(new WorkflowChild(
                        workflow.Activities[i].Name,
                        workflow.Activities[i].Label,
                        workflow.Activities[i].Description,
                        0));
                }

                ctrWorkflowChildren.Children = activityNames.ToArray();
                return;
            }

            ctrWorkflowChildren.WorkflowName = "";
            ctrWorkflowChildren.Children = new List<WorkflowChild>().ToArray(); 
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ctrWorkflowChildrenActivities control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ctrWorkflowChildrenActivities_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnSelectedActivityChanged != null)
            {
                WorkflowChild workflowChild = this.ctrWorkflowChildren.SelectedNode.Tag as WorkflowChild;
                string activityName = workflowChild == null ? string.Empty : workflowChild.Name;
                OnSelectedActivityChanged(this, new SelectedActivityChangedEventArgs(activityName));
            }
        }

        /// <summary>
        /// Handles the DoubleClick event of the ctrWorkflowChildrenActivities control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ctrWorkflowChildrenActivities_DoubleClick(object sender, EventArgs e)
        {
            if (OnActivityDoubleClick != null)
            {
                WorkflowChild workflowChild = this.ctrWorkflowChildren.SelectedNode.Tag as WorkflowChild;
                string activityName = workflowChild == null ? string.Empty : workflowChild.Name;
                OnActivityDoubleClick(this, new SelectedActivityChangedEventArgs(activityName));
            }
        }
    }
}
